#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define ENTRY_TYPE intlist
#define ENTRY_MEMBER list
#include "../include/z_list.h"

struct intlist {
	int n;
	struct list_head list;

};

typedef struct intlist intlist;

int main(void) {
	init_list(int_list);
	struct list_head * head = &int_list;
	intlist arr[] = { {.n = 1}, {.n = 2}, {.n = 3}  };

	list_add_entry(head,&arr[0]);
	list_add_entry(head,&arr[1]);
	list_add_entry(head,&arr[2]);
	printf("len = %lu\n", list_length(head));

	assert(list_entry(&(arr[0].list)) == &arr[0]);

	assert(list_next_entry(head,&arr[1])->n == 1);
	assert(list_prev_entry(head,&arr[1])->n == 3);

	intlist tmp = *list_entry(list_pop(head));
	assert(tmp.n == 3);
	assert(list_length(head) == 2);

	list_push(head,&arr[2].list);
	assert(list_length(head) == 3);

	list_foreach_entry(head,itr) {
		printf("node.n = %d\n",itr->n);

	}
	list_foreach_prev(head,itr) {
		intlist * i = list_entry(itr);
		printf("node.n = %d\n",i->n);

	}

	intlist * n1 = (intlist*)malloc(sizeof(intlist));
	intlist * n2 = (intlist*)malloc(sizeof(intlist));
	intlist * n3 = (intlist*)malloc(sizeof(intlist));
	intlist * n4 = (intlist*)malloc(sizeof(intlist));
	init_list(int_list2);
	struct list_head * head2 = &int_list2;
	list_add_entry(head2,n1);
	list_add_entry(head2,n2);
	list_add_entry(head2,n3);
	int i = 1;
	list_foreach_entry_reverse(head2,itr) {
		itr->n = i;
		i++;

	}
	n4->n = 4;
	list_enqueue(head2,&n4->list);
	list_foreach_entry_reverse(head2,itr) {
		printf("node.n = %d\n",itr->n);

	}
	struct list_head *tmp_itr;
	list_foreach_safe(head2,itr,tmp_itr) {
		intlist * i = list_entry(itr);
		list_del(head2);
		free(i);

	}
	assert(list_empty(head2));

}
